package com.gegewa.topic.file.controller;

import com.gegewa.topic.file.service.FileService;
import com.gegewa.topic.response.DataResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("file")
public class FileController {

    private final static Logger LOGGER = LoggerFactory.getLogger(FileController.class);

    @Resource
    private FileService fileService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "file/upload";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public DataResponse<String> uploadFile(HttpServletRequest request) {
        try {
            Long flag = System.nanoTime();
            LOGGER.info("{}-上传文件-入参：request={}", flag, request);
            List<String> filePathList = fileService.uploadFile(request, "/Users/nali/Documents/upload/");
            DataResponse dataResponse = DataResponse.success(filePathList.get(0));
            LOGGER.info("{}-上传文件-返回：dataResponse={}", flag, dataResponse);
            return dataResponse;
        } catch (Exception e) {
            return DataResponse.fail("file upload failed");
        }
    }

}
